<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class bank extends template_plugin
{
	var $ipsclass;
	
	var $name = "Bank";
	var $multiple = 0;
	var $cache_array = array('ibm_bank_starter' => array('Bank starter cache','bank_cache'));
	
	var $settings = array();
	
	function _load(&$plugin)
	{
		parent::_load($plugin);
		
		if($plugin['link'])
		{
			$this->settings = unserialize($plugin['plugin_config']);
		}
		else
		{
			$this->settings['max_account'] = 0;
		}
	}
	
	function bank_cache()
	{
		$this->ipsclass->cache['ibm_bank_starter'] = array();
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_bank_starter'));
		$this->ipsclass->DB->exec_query();
		while($starter = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->cache['ibm_bank_starter'][$starter['starter_id']] = $starter;
		}
		
		$this->ipsclass->update_cache( array( 'name' => 'ibm_bank_starter', 'array' => 1, 'deletefirst' => 1 ) );
	}
	
	function acp_menu()
	{
		return array(
					  1 => array('Manage Bank Starters','code=plugin&do=run&link='.$this->plugin['link'].'&action=starter'),
					  2 => array('Manage Bank Accounts','code=plugin&do=run&link='.$this->plugin['link'].'&action=accounts'),
					  3 => array('View Transactions','code=plugin&do=run&link='.$this->plugin['link'].'&action=logs'),
					);
	}
	
	function acp()
	{
		$redirect = array(
							'starter' => 'manage_starter',
							'accounts' => 'manage_accounts',
							'logs' => 'view_transactions',
							'export' => 'export',
						);
		
		if(!isset($redirect[$this->ipsclass->input['action']]))
		{
			$this->ipsclass->input['action'] = 'starter';
		}
		
		$function = 'acp_'.$redirect[$this->ipsclass->input['action']];
		$this->$function();
	}
	
	function acp_manage_starter()
	{
		// Yes redirect again >_>
		$redirect = array(
								'list' => 'list',
								'add' => 'add',
								'process' => 'process',
								'edit' => 'save',
							);
		if(!$redirect[$this->ipsclass->input['runme']])
		{
			$this->ipsclass->input['runme'] = 'list';
		}
		
		$function = 'acp_starter_'.$redirect[$this->ipsclass->input['runme']];
		$this->$function();
	}
	
	function acp_starter_list()
	{
		$this->ipsclass->admin->page_title  = "Welcome to managing the Bank Account Starters";
		$this->ipsclass->admin->page_detail = "Welcome to managing the Bank Account Starters. Here you can add/edit/delete bank account.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;action=starter', 'Manage Starter' );
		
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','starter'),
																	array('runme','process'),
															)	);
		
		$this->ipsclass->adskin->td_header[] = array( "Name"                   , "20%" );
		$this->ipsclass->adskin->td_header[] = array( "Description"            , "35%" );
		$this->ipsclass->adskin->td_header[] = array( "Base Interest"          , "10%" );
		$this->ipsclass->adskin->td_header[] = array( "Interest Time"          , "20%" );
		$this->ipsclass->adskin->td_header[] = array( "Edit/Delete"            , "10%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Account Starter List");
		
		if(is_array($this->ipsclass->cache['ibm_bank_starter']) and count($this->ipsclass->cache['ibm_bank_starter']))
		{
			foreach($this->ipsclass->cache['ibm_bank_starter'] as $starter)
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																					$starter['s_name'],
																					$starter['s_description'],
																					$starter['s_interest_rate'],
																					round($starter['s_interest_time']/3600,3)." hours",
																					$this->ipsclass->adskin->form_checkbox('starter['.$starter['starter_id'].']'),
																			)	);
			}
		}
		else
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No Bank Starter made yet",'center');
		}
		
		$this->ipsclass->html .= "<tr><td align='left' class='tablesubheader' colspan='3'><input type='submit' value='Edit Account Starter' name='button_edit' id='button'></td><td align='right' class='tablesubheader' colspan='3'><input type='submit' value='Delete Account Starter' name='button_delete' id='button'></td></tr></form>\n";
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','starter'),
																	array('runme','add'),
														),'theAdminForm',"onsubmit='return ValidateForm()'");
		
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Add an Account Starter");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Name</b><br />The name of the account starter.",
																			  $this->ipsclass->adskin->form_input("s_name")
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("<b>Description</b>: The description for the account starter.<br />".$this->ipsclass->han_editor->show_editor( "", 's_description' ), 'left', 'tablerow1');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Interest rate</b><br />How much interest is applied to the bank accounts?<br><i>In percent, up to two decimal places</i>",
																			  $this->ipsclass->adskin->form_simple_input("s_interest_rate","0.00")."%"
																	 )      );
								 									
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Interest time</b><br />How much time for each interest is applied to the bank accounts?<br><i>In hours</i>",
																			  $this->ipsclass->adskin->form_simple_input("s_interest_time/3600",1)." hours"
																	 )      );
								 									
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Desposit Percentage Fee</b><br />Percent of the amount going to be desposited as a fee. This is also known as relative processing fee. Also going to be added to the previous fee.<br><i>In percent</i>",
																			  $this->ipsclass->adskin->form_simple_input("s_deposit_fee","0.00")."%"
																	 )      );
								 									
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Withdraw Percentage Fee</b><br />Percent of the amount going to be withdrawn as a fee. This is also known as relative processing fee. Also going to be added to the previous fee.<br><i>In percent</i>",
																			  $this->ipsclass->adskin->form_simple_input("s_withdraw_fee","0.00")."%"
																	 )      );
								 									
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Bank Account Creation Fee</b><br />How much money as a fee to create a single bank account.",
																			  $this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input("create_price",0).$this->ipsclass->vars['market_suffix']
																	 )      );
								 									
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Bank Account Deletion Fee</b><br />How much money as a fee to remove a single bank account.<br />",
																			  $this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input("delete_fee",0).$this->ipsclass->vars['market_suffix']
																	 )      );
								 									
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Add account starter");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_starter_add()
	{
		$this->ipsclass->DB->do_insert('market_bank_starter',array(
																		's_name' => $this->ipsclass->input['s_name'],
																		's_description' => $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 's_description' ) ) ),
																		's_interest_time' => $this->ipsclass->numbers->multiply($this->ipsclass->input['s_interest_time/3600'],3600),
																		's_interest_rate' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['s_interest_rate'],'float'),
																		's_deposit_fee' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['s_deposit_fee'],'float'),
																		's_withdraw_fee' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['s_withdraw_fee'],'float'),
																		'create_price' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['create_price'],'int'),
																		'delete_fee' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['delete_fee'],'int'),
										)							);
		
		$this->bank_cache();
		
		$this->ipsclass->admin->save_log("ibMarket: Bank Account Starter Added(".$this->ipsclass->input['s_name'].")");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js,"Bank account starter, ".$this->ipsclass->input['s_name'].", added...");
	}
	
	function acp_starter_process()
	{
		if(is_array($this->ipsclass->input['starter']))
		{
			foreach($this->ipsclass->input['starter'] as $s_id => $check)
			{
				if(!$check || !isset($this->ipsclass->cache['ibm_bank_starter'][$s_id]))
				{
					unset($this->ipsclass->input['starter']);
				}
			}
		}
		if(!is_array($this->ipsclass->input['starter']) || count($this->ipsclass->input['starter']) == 0)
		{
			$this->ipsclass->admin->error("You must select an account starter to deal with");
		}
		if($this->ipsclass->input['button_delete'])
		{
			$this->acp_starter_delete();
		}
		else
		{
			$this->acp_starter_edit();
		}
	}
	
	function acp_starter_edit()
	{
		$this->ipsclass->admin->page_title  = "Welcome to editing the Bank Account Starters";
		$this->ipsclass->admin->page_detail = "Welcome to editing the Bank Account Starters.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;action=starter', 'Manage Starter' );
		$this->ipsclass->admin->nav[] 		= array( ''                                              , 'Edit Starter'   );
		
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','starter'),
																	array('runme','edit'),
														),'theAdminForm',"onsubmit='return ValidateForm()'");
		
		foreach(array_keys($this->ipsclass->input['starter']) as $s_id)
		{
			if ( $this->ipsclass->han_editor->method == 'rte' )
			{
				$this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_description'] = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_description'] ) );
				$this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_description'] = $this->ipsclass->parser->convert_ipb_html_to_html( $this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_description'] );
			}
			else
			{
				$this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_description'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_description'] ) );
			}
			
			$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
			$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
			
			$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Account Starter");
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Name</b><br />The name of the account starter.",
																				  $this->ipsclass->adskin->form_input("starter[".$s_id."][s_name]",$this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_name'])
																		 )      );
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("<b>Description</b>: The description for the account starter.<br />".$this->ipsclass->han_editor->show_editor( $this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_description'], 'starter_'.$s_id.'_s_description' ), 'left', 'tablerow1');
			
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Interest rate</b><br />How much interest is applied to the bank accounts?<br><i>In percent, up to two decimal places</i>",
																				  $this->ipsclass->adskin->form_simple_input("starter[".$s_id."][s_interest_rate]",$this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_interest_rate'])."%"
																		 )      );
									 									
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Interest time</b><br />How much time for each interest is applied to the bank accounts?<br><i>In hours</i>",
																				  $this->ipsclass->adskin->form_simple_input("starter[".$s_id."][s_interest_time/3600]",$this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_interest_time']/3600)." hours"
																		 )      );
									 									
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Desposit Percentage Fee</b><br />Percent of the amount going to be desposited as a fee. This is also known as relative processing fee. Also going to be added to the previous fee.<br><i>In percent</i>",
																				  $this->ipsclass->adskin->form_simple_input("starter[".$s_id."][s_deposit_fee]",$this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_deposit_fee'])."%"
																		 )      );
									 									
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Withdraw Percentage Fee</b><br />Percent of the amount going to be withdrawn as a fee. This is also known as relative processing fee. Also going to be added to the previous fee.<br><i>In percent</i>",
																				  $this->ipsclass->adskin->form_simple_input("starter[".$s_id."][s_withdraw_fee]",$this->ipsclass->cache['ibm_bank_starter'][$s_id]['s_withdraw_fee'])."%"
																		 )      );
									 									
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Bank Account Creation Fee</b><br />How much money as a fee to create a single bank account.",
																				  $this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input("starter[".$s_id."][create_price]",$this->ipsclass->cache['ibm_bank_starter'][$s_id]['create_price']).$this->ipsclass->vars['market_suffix']
																		 )      );
									 									
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Bank Account Deletion Fee</b><br />How much money as a fee to remove a single bank account.<br />",
																				  $this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input("starter[".$s_id."][delete_fee]",$this->ipsclass->cache['ibm_bank_starter'][$s_id]['delete_fee']).$this->ipsclass->vars['market_suffix']
																		 )      );
									 									
			$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form_standalone("Edit account starter(s)");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_starter_save()
	{
		if(!is_array($this->ipsclass->input['starter']) || count($this->ipsclass->input['starter']) == 0)
		{
			$this->ipsclass->admin->error("Unknown error");
		}
		foreach($this->ipsclass->input['starter'] as $s_id => $info)
		{
			if(!isset($this->ipsclass->cache['ibm_bank_starter'][$s_id]))
			{
				continue;
			}
			
			$this->ipsclass->DB->do_update('market_bank_starter',array(
																			's_name' => $info['s_name'],
																			's_description' => $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 'starter_'.$s_id.'_s_description' ) ) ),
																			's_interest_time' => $this->ipsclass->numbers->multiply($info['s_interest_time/3600'],3600),
																			's_interest_rate' => $this->ipsclass->numbers->parse_input_number($info['s_interest_rate'],'float'),
																			's_deposit_fee' => $this->ipsclass->numbers->parse_input_number($info['s_deposit_fee'],'float'),
																			's_withdraw_fee' => $this->ipsclass->numbers->parse_input_number($info['s_withdraw_fee'],'float'),
																			'create_price' => $this->ipsclass->numbers->parse_input_number($info['create_price'],'int'),
																			'delete_fee' => $this->ipsclass->numbers->parse_input_number($info['delete_fee'],'int'),
											),"starter_id=".$s_id	);
		}
		
		$this->bank_cache();
		
		$this->ipsclass->admin->save_log("ibMarket: Bank Account Starters Edited");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js,"Bank account starters edited...");
	}
	
	function acp_starter_delete()
	{
		$s_ids = array_keys($this->ipsclass->input['starter']);
		
		$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_bank_starter','where'=>"starter_id IN (".implode(", ", $s_ids).")"));
		
		$this->bank_cache();
		
		$this->ipsclass->admin->save_log("ibMarket: Bank Account Starters Deleted");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js,"Bank account starters deleted...");
	}
	
	function acp_manage_accounts()
	{
		// Yes redirect again >_>
		$redirect = array(
								'search' => 'search',
								'find' => 'find',
								'form' => 'form',
								'save' => 'save',
								'delete' => 'delete',
							);
		if(!$redirect[$this->ipsclass->input['runme']])
		{
			$this->ipsclass->input['runme'] = 'search';
		}
		
		$function = 'acp_manage_'.$redirect[$this->ipsclass->input['runme']];
		$this->$function();
	}
	
	function acp_manage_search()
	{
		$this->ipsclass->admin->page_title  = "Welcome to managing the Bank Accounts";
		$this->ipsclass->admin->page_detail = "Welcome to managing the Bank Accounts. Here you have to search for which bank account to edit or delete.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;action=accounts', 'Search Accounts' );
		
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','accounts'),
																	array('runme','find'),
											),'memberlookup');
		
		$this->ipsclass->html .= "<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:100'></div>\n";

		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Look for Bank accounts by Member Name");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Name</b><br />The name of the member to look for.",
																			  $this->ipsclass->adskin->form_input("member_name","",'text',"autocomplete='off' style='width:210px' id='member_name'")
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Search");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= "<script type=\"text/javascript\" src='{$this->ipsclass->vars['board_url']}/jscripts/ipb_xhr_findnames.js'></script>\n";
		$this->ipsclass->html .= "<script type=\"text/javascript\">\n";
		$this->ipsclass->html .= "	init_js( 'memberlookup', 'member_name');\n";
		$this->ipsclass->html .= "	var tmp = setTimeout( 'main_loop()', 10 );\n";
		$this->ipsclass->html .= "</script>\n";
		
		$this->ipsclass->admin->output();
	}
	
	function acp_manage_find()
	{
		$this->ipsclass->admin->page_title  = "Welcome to managing the Bank Accounts";
		$this->ipsclass->admin->page_detail = "Welcome to managing the Bank Accounts. Here you which one bank account to edit or delete.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;action=accounts', 'Search Bank Accounts' );
		$this->ipsclass->admin->nav[] 		= array( ''                                               , 'List Bank Accounts'   );
		
		$mem = $this->ipsclass->DB->build_and_exec_query(array('select'=>'id,members_display_name','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		
		if(!$mem)
		{
			$this->ipsclass->admin->error("The member does not exist.");
		}
		
		$this->ipsclass->adskin->td_header[] = array( "ID"          , "30%" );
		$this->ipsclass->adskin->td_header[] = array( "Balance"     , "20%" );
		$this->ipsclass->adskin->td_header[] = array( "Edit"        , "10%" );
		$this->ipsclass->adskin->td_header[] = array( "Delete"      , "10%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Accounts by ".$mem['members_display_name']);
		
		$this->ipsclass->DB->build_query(array(
													'select' => '*',
													'from' => 'market_bank_accounts',
													'where' => "a_mem_id=".$mem['id'],
													'order' => 'balance DESC',
										)		);
		$this->ipsclass->DB->exec_query();
		while($account = $this->ipsclass->DB->fetch_row())
		{
			$id = $this->_build_bank_account_id($account['account_id'],$account['a_random'],$account['a_mem_id']);
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  $id,
																				  $this->ipsclass->numbers->format_currency($account['balance']),
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=accounts&amp;runme=form&amp;account_id=".$id."'>Edit</a>",
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=accounts&amp;runme=delete&amp;account_id=".$id."'>Delete</a>",
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_manage_form()
	{
		$this->ipsclass->admin->page_title  = "Welcome to managing the Bank Accounts";
		$this->ipsclass->admin->page_detail = "Welcome to managing the Bank Accounts. Here you edit one bank account.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;action=accounts', 'Search Bank Accounts' );
		$this->ipsclass->admin->nav[] 		= array( ''                                               , 'List Bank Accounts'   );
		$this->ipsclass->admin->nav[] 		= array( ''                                               , 'Edit Bank Account'    );
		
		$ids = $this->_revert_id($this->ipsclass->input['account_id']);
		
		$account = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$ids['account']));
		
		if(!$account || $ids['random'] != $account['a_random'] || $ids['member'] != $account['a_mem_id'])
		{
			$this->ipsclass->admin->error("The account does not exists.");
		}
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$account['a_mem_id']));
		
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','accounts'),
																	array('runme','save'),
																	array('account',$this->ipsclass->input['account_id']),
											),'memberlookup');
		
		$this->ipsclass->html .= "<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:100'></div>\n";
		
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Edit Bank Account");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Account Name</b><br />The name of the bank account.",
																			  $this->ipsclass->adskin->form_input("a_name",$account['a_name'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Owner's Name</b><br />The name of the member that this bank account belongs to.",
																			  $this->ipsclass->adskin->form_input("member_name",$member['members_display_name'],'text',"autocomplete='off' style='width:210px' id='member_name'")
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Balance</b><br />The money in this bank account.",
																			  $this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("balance",$account['balance']))
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Interest Time</b><br />The amount of time between each interest.",
																			  $this->ipsclass->adskin->form_simple_input("a_interest_time/3600",$account['a_interest_time']/3600)." hours"
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Interest Rate</b><br />The money in this bank account.",
																			  $this->ipsclass->adskin->form_simple_input("interest_rate",$account['interest_rate'])."%"
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Adjust last interest time</b><br />To move the time forward, just input a positive number. To move the time back, just input a negative number.<br />Last Interest: ".$this->ipsclass->get_date($account['a_last_interest'],'LONG'),
																			  $this->ipsclass->adskin->form_simple_input("adjust_last_time",0)." minutes"
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Deposit Fee</b><br />The deposit fee on this account.",
																			  $this->ipsclass->adskin->form_simple_input("a_deposit_fee",$account['a_deposit_fee'])."%"
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Withdraw Fee</b><br />The withdraw fee on this account.",
																			  $this->ipsclass->adskin->form_simple_input("a_withdraw_fee",$account['a_withdraw_fee'])."%"
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Deletion Fee</b><br />The delete fee on this account.",
																			  $this->ipsclass->adskin->form_simple_input("a_delete_fee",$account['a_delete_fee'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Save");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= "<script type=\"text/javascript\" src='{$this->ipsclass->vars['board_url']}/jscripts/ipb_xhr_findnames.js'></script>\n";
		$this->ipsclass->html .= "<script type=\"text/javascript\">\n";
		$this->ipsclass->html .= "	init_js( 'memberlookup', 'member_name');\n";
		$this->ipsclass->html .= "	var tmp = setTimeout( 'main_loop()', 10 );\n";
		$this->ipsclass->html .= "</script>\n";
		
		$this->ipsclass->admin->output();
	}
	
	function acp_manage_save()
	{
		$ids = $this->_revert_id($this->ipsclass->input['account']);
		
		$account = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$ids['account']));
		
		if(!$account || $ids['random'] != $account['a_random'] || $ids['member'] != $account['a_mem_id'])
		{
			$this->ipsclass->admin->error("The account does not exists.");
		}
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name']."'")));
		if(!$member)
		{
			$this->ipsclass->admin->error("Cannot find that member");
		}
		
		$this->ipsclass->DB->do_update('market_bank_accounts',array(
																		'a_mem_id' => $member['id'],
																		'a_name' => $this->ipsclass->input['a_name'],
																		'balance' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['balance'],'int'),
																		'a_interest_time' => $this->ipsclass->numbers->multiply($this->ipsclass->input['a_interest_time/3600'],3600),
																		'a_last_interest' => $account['a_last_interest'] + $this->ipsclass->numbers->multiply($this->ipsclass->input['adjust_last_time'],60),
																		'interest_rate' => $this->ipsclass->input['interest_rate'],
																		'a_deposit_fee' => $this->ipsclass->input['a_deposit_fee'],
																		'a_withdraw_fee' => $this->ipsclass->input['a_withdraw_fee'],
																		'a_delete_fee' => $this->ipsclass->input['a_delete_fee'],
									),"account_id=".$ids['account']);
		
		$this->ipsclass->admin->save_log("ibMarket: Bank Account Edited(".$this->ipsclass->input['account'].")");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=accounts&runme=find&member_name=".$member['members_l_display_name'],"Bank account edited...");
	}
	
	function acp_manage_delete()
	{
		$ids = $this->_revert_id($this->ipsclass->input['account_id']);
		
		$account = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$ids['account']));
		
		if(!$account || $ids['random'] != $account['a_random'] || $ids['member'] != $account['a_mem_id'])
		{
			$this->ipsclass->admin->error("The account does not exists.");
		}
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$account['a_mem_id']));
		
		$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_bank_accounts','where'=>"account_id=".$ids['account']));
		
		$this->ipsclass->admin->save_log("ibMarket: Bank Account Deleted(".$this->ipsclass->input['account'].")");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=accounts&runme=find&member_name=".$member['members_l_display_name'],"Bank account deleted...");
	}
	
	function acp_view_transactions()
	{
		$LIMIT = 50;
		
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$this->ipsclass->admin->page_title  = "Welcome to transaction page for the Bank";
		$this->ipsclass->admin->page_detail = "Welcome to transaction page for all bank accounts";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;action=logs', 'View Transactions' );
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_bank_transactions'));
		$page = $this->ipsclass->adskin->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'USE_ST'       => 'st',
																	'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																	'L_SINGLE'     => "Single Page",
																	'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=logs"
															)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "Account ID"  , "35%" );
		$this->ipsclass->adskin->td_header[] = array( "IP Address"  , "15%" );
		$this->ipsclass->adskin->td_header[] = array( "Time"        , "15%" );
		$this->ipsclass->adskin->td_header[] = array( "Action"      , "35%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Transaction Logs");
		
		$this->ipsclass->DB->build_query(array(
													'select' => '*',
													'from' => 'market_bank_transactions',
													'order' => 'l_timestamp DESC',
													'limit' => array($this->ipsclass->input['st'],$LIMIT)
										)		);
		$this->ipsclass->DB->exec_query();
		while($log = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  $log['l_account_id'],
																				  $log['l_ip_address'],
																				  $this->ipsclass->get_date($log['l_timestamp'],'LONG'),
																				  ucfirst($log['l_action']),
																		 )      );
		}
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No bank transaction",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_settings()
	{
		$html = "";
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Maximum number of bank accounts for a member?</b><br />Input 0 for no limit on the number of bank accounts",
															$this->ipsclass->adskin->form_simple_input('max_account',$this->settings['max_account']),
													)	);
		
		return $html;
	}
	
	function acp_save_settings(&$input)
	{
		$save = array();
		$save['max_account'] = intval($this->ipsclass->input['max_account']);
		
		$input['plugin_config'] = serialize($save);
	}
	
	function acp_install()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/sql/'.$this->ipsclass->vars['sql_driver'].'_plugin_queries.php','sql_plugin_queries');
		
		$this->ipsclass->DB->build_query_from_cache('table_bank_starter',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_bank_account',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_bank_transactions',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		
		$this->bank_cache();
	}
	
	function acp_uninstall()
	{
		$this->ipsclass->DB->sql_drop_table( 'market_bank_accounts'     );
		$this->ipsclass->DB->sql_drop_table( 'market_bank_starter'      );
		$this->ipsclass->DB->sql_drop_table( 'market_bank_transactions' );
	}
	
	function run()
	{
		if($this->ipsclass->input['request_method'] != 'post')
		{
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"a_interest_time+a_last_interest < ".time()));
			$query = $this->ipsclass->DB->exec_query();
			while($account = $this->ipsclass->DB->fetch_row($query))
			{
				$new_balance = $this->ipsclass->numbers->round($this->ipsclass->numbers->multiply($account['balance'],1+$account['interest_rate']/100));
				$increase = $this->ipsclass->numbers->subtract($new_balance,$account['balance']);
				$this->ipsclass->DB->do_update('market_bank_accounts',array(
																				'balance' => $this->ipsclass->numbers->round($this->ipsclass->numbers->multiply($account['balance'],1+$account['interest_rate']/100)),
																				'a_last_interest' => $account['a_last_interest'] + $account['a_interest_time'],
									),"account_id=".$account['account_id']);
				$this->_log_transaction($this->_build_bank_account_id($account['account_id'],$account['a_random'],$account['a_mem_id']),'interest',$increase,$new_balance,0,$account['a_last_interest'] + $account['a_interest_time']);
			}
		}
		
		$this->ipsclass->load_template('skin_market_plugin');
		$this->ipsclass->load_language('lang_market_plugin');
		
		$this->ipsclass->market_class->title[] = array($this->plugin['name'],'market_'.$this->plugin['link']);
		if(in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->ipsclass->lang['bank_manage_account']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}&amp;do=create'>".$this->ipsclass->lang['bank_create_account']."</a></b>",
														);
			
			$do_redirect = array(
									'create' => 'create',
									'manage' => 'manage',
									'account' => 'process_account',
									'xml' => 'xml',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'manage';
			}
		}
		else
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}&amp;do=create'>".$this->ipsclass->lang['bank_view_account_starters']."</a></b>",
														);
			
			$do_redirect = array(
									'create' => 'create',
									'xml' => 'xml',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'create';
			}
		}
		
		$func = 'run_'.$do_redirect[$this->ipsclass->input['do']];
		$this->$func();
		
		$this->ipsclass->output = str_replace("\\\\\\n","\\n",$this->ipsclass->output);
	}
	
	function run_manage()
	{
		$this->ipsclass->subtitle = $this->ipsclass->lang['bank_manage_account'];
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->ipsclass->lang['bank_manage_account']."</a>");
		
		$accounts = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"a_mem_id=".$this->ipsclass->member['id'],'order'=>'balance DESC'));
		$this->ipsclass->DB->exec_query();
		while($account = $this->ipsclass->DB->fetch_row())
		{
			$account['id'] = $this->_build_bank_account_id($account['account_id'],$account['a_random'],$this->ipsclass->member['id']);
			$account['balance'] = $this->ipsclass->numbers->format_currency($account['balance']);
			$accounts[] = $account;
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->bank_list_accounts($accounts);
	}
	
	function run_create()
	{
		$this->ipsclass->input['starter'] = intval($this->ipsclass->input['starter']);
		
		if($this->ipsclass->input['request_method'] == 'post' && is_array($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]) && in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			$this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price'] = $this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price'],$this->ipsclass->member['market_deduct']);
			
			// Check Max accounts here
			if($this->ipsclass->numbers->compare($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price'],$this->ipsclass->member[$this->ipsclass->vars['market_members_field']]) > 0)
			{
				$this->ipsclass->Error(array('MSG'=>'bank_notenoughmoney'));
			}
			
			if($this->settings['max_account'])
			{
				$count = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_bank_accounts','where'=>"a_mem_id=".$this->ipsclass->member['id']));
				
				if($count['total'] >= $this->settings['max_account'])
				{
					$this->ipsclass->Error(array('MSG'=>'bank_toomanyaccounts'));
				}
			}
			
			if(!$this->ipsclass->input['name'])
			{
				$this->ipsclass->Error(array('MSG'=>'bank_need_name'));
			}
			
			$random_id = $this->ipsclass->numbers->multiply(1000000,(float)microtime());
			
			$current_time = time();
			
			$this->ipsclass->DB->do_insert('market_bank_accounts',array(
																			'a_random' => $random_id,
																			'a_mem_id' => $this->ipsclass->member['id'],
																			'a_name' => $this->ipsclass->input['name'],
																			'a_open' => $current_time,
																			'balance' => 0,
																			'a_interest_time' => $this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_interest_time'],
																			'a_last_interest' => $current_time - $current_time % 3600,
																			'interest_rate' => $this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_interest_rate'],
																			'a_deposit_fee' => $this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_deposit_fee'],
																			'a_withdraw_fee' => $this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_withdraw_fee'],
																			'a_delete_fee' => $this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['delete_fee'],
											)							);
			
			$account_id = $this->ipsclass->DB->get_insert_id();
			
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price']),
					),"id=".$this->ipsclass->member['id']);
			
			$this->_log_transaction($this->_build_bank_account_id($account_id,$random_id,$this->ipsclass->member['id']),'create',0,0,$this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price']);
			
			$this->ipsclass->market_class->redirect('bank_account_created',"plugin=".$this->ipsclass->input['plugin']."&do=manage&account=".$this->_build_bank_account_id($account_id, $random_id, $this->ipsclass->member['id']));
		}
		else
		{
			if(in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
			{
				$this->ipsclass->subtitle = $this->ipsclass->lang['bank_view_account_starters'];
				$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}&amp;do=create'>".$this->ipsclass->lang['bank_view_account_starters']."</a>");
			}
			else
			{
				$this->ipsclass->subtitle = $this->ipsclass->lang['bank_create_account'];
				$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}&amp;do=create'>".$this->ipsclass->lang['bank_create_account']."</a>");
			}
			
			$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->bank_create_account();
		}
	}
	
	function run_process_account()
	{
		$info = $this->_revert_id($this->ipsclass->input['account_id']);
		
		if($info['member'] == $this->ipsclass->member['id'] && $account = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$info['account']." AND a_random=".$info['random']." AND a_mem_id=".$info['member'])))
		{
			$this->ipsclass->input['amount'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['amount'],'int');
			
			if($this->ipsclass->input['action'] == 'withdraw')
			{
				$account['a_withdraw_fee'] = $this->ipsclass->numbers->multiply($account['a_withdraw_fee'],$this->ipsclass->member['market_deduct']);
				$fee = $this->ipsclass->numbers->round($this->ipsclass->numbers->divide($this->ipsclass->numbers->multiply($this->ipsclass->input['amount'],$account['a_withdraw_fee']),100));
				
				$balance = $this->ipsclass->numbers->subtract($account['balance'],$this->ipsclass->input['amount']);
				$money = $this->ipsclass->numbers->subtract($this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->input['amount']),$fee);
				
				if($this->ipsclass->numbers->compare($this->ipsclass->input['amount'],0) < 0 or $this->ipsclass->numbers->compare($balance,0) < 0 or $this->ipsclass->numbers->compare($money,0) < 0)
				{
					$this->ipsclass->Error(array('MSG'=>'bank_notenoughmoney'));
				}
				
				$this->ipsclass->DB->do_update('market_bank_accounts',array('balance'=>$balance),"account_id=".$info['account']);
				$this->ipsclass->DB->do_update('members',array(
																$this->ipsclass->vars['market_members_field'] => $money,
						),"id=".$this->ipsclass->member['id']);
				
				$this->_log_transaction($this->ipsclass->input['account_id'],'withdraw',$this->ipsclass->input['amount'],$balance,$fee);
				
				$this->ipsclass->market_class->redirect('bank_finish_withdraw',"plugin=".$this->ipsclass->input['plugin']."&do=manage&account=".$this->ipsclass->input['account_id']);
			}
			elseif($this->ipsclass->input['action'] == 'deposit')
			{
				$account['a_deposit_fee'] = $this->ipsclass->numbers->multiply($account['a_deposit_fee'],$this->ipsclass->member['market_deduct']);
				$fee = $this->ipsclass->numbers->round($this->ipsclass->numbers->divide($this->ipsclass->numbers->multiply($this->ipsclass->input['amount'],$account['a_deposit_fee']),100));
				
				$balance = $this->ipsclass->numbers->add($account['balance'],$this->ipsclass->input['amount']);
				$money = $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->add($this->ipsclass->input['amount'],$fee));
				
				if($this->ipsclass->numbers->compare($this->ipsclass->input['amount'],0) < 0 or $this->ipsclass->numbers->compare($money,0) < 0)
				{
					$this->ipsclass->Error(array('MSG'=>'bank_notenoughmoney'));
				}
				
				$this->ipsclass->DB->do_update('market_bank_accounts',array('balance'=>$balance),"account_id=".$info['account']);
				$this->ipsclass->DB->do_update('members',array(
																$this->ipsclass->vars['market_members_field'] => $money,
						),"id=".$this->ipsclass->member['id']);
				
				$this->_log_transaction($this->ipsclass->input['account_id'],'deposit',$this->ipsclass->input['amount'],$balance,$fee);
				
				$this->ipsclass->market_class->redirect('bank_finish_deposit',"plugin=".$this->ipsclass->input['plugin']."&do=manage&account=".$this->ipsclass->input['account_id']);
			}
			elseif($this->ipsclass->input['action'] == 'remove')
			{
				$account['a_delete_fee'] = $this->ipsclass->numbers->multiply($account['a_delete_fee'],$this->ipsclass->member['market_deduct']);
				$money = $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$account['a_delete_fee']);
				
				if($this->ipsclass->numbers->compare($money,0) < 0)
				{
					$this->ipsclass->Error(array('MSG'=>'bank_notenoughmoney'));
				}
				if($this->ipsclass->numbers->compare($account['balance'],0) != 0)
				{
					$this->ipsclass->Error(array('MSG'=>'bank_accountnotempty'));
				}
				
				$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_bank_accounts','where'=>"account_id=".$info['account']));
				$this->ipsclass->DB->do_update('members',array(
																$this->ipsclass->vars['market_members_field'] => $money,
						),"id=".$this->ipsclass->member['id']);
				
				$this->_log_transaction($this->ipsclass->input['account_id'],'remove',0,0,$account['a_delete_fee']);
				
				$this->ipsclass->market_class->redirect('bank_finish_remove',"plugin=".$this->ipsclass->input['plugin']);
			}
		}
		$this->ipsclass->Error(array('MSG'=>'bank_invalid_transaction'));
	}
	
	function run_xml()
	{
		$response = "";
		
		if($this->ipsclass->input['info'] == 'starter')
		{
			$this->ipsclass->input['starter'] = intval($this->ipsclass->input['starter']);
			
			$this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price'] = $this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price'],$this->ipsclass->member['market_deduct']);
			
			$response .= $this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price'];
			$response .= "\n";
			$response .= $this->ipsclass->numbers->format_currency($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['create_price']);
			$response .= "\n";
			$response .= $this->ipsclass->numbers->do_number_format($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_interest_rate'],10);
			$response .= "\n";
			$response .= $this->_interest_time($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_interest_time']);
			$response .= "\n";
			$response .= $this->ipsclass->numbers->do_number_format($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_deposit_fee'],10);
			$response .= "\n";
			$response .= $this->ipsclass->numbers->do_number_format($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_withdraw_fee'],10);
			$response .= "\n";
			$response .= $this->ipsclass->numbers->format_currency($this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['delete_fee']);
			$response .= "\n";
			$response .= $this->ipsclass->cache['ibm_bank_starter'][$this->ipsclass->input['starter']]['s_description'];
		}
		elseif(in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			if($this->ipsclass->input['info'] == 'account')
			{
				$info = $this->_revert_id($this->ipsclass->input['account']);
				
				if($info['member'] == $this->ipsclass->member['id'] && $account = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$info['account']." AND a_random=".$info['random']." AND a_mem_id=".$info['member'])))
				{
					$account['a_delete_fee'] = $this->ipsclass->numbers->multiply($account['a_delete_fee'],$this->ipsclass->member['market_deduct']);
					$account['a_deposit_fee'] = $this->ipsclass->numbers->multiply($account['a_deposit_fee'],$this->ipsclass->member['market_deduct']);
					$account['a_withdraw_fee'] = $this->ipsclass->numbers->multiply($account['a_withdraw_fee'],$this->ipsclass->member['market_deduct']);
					
					$response .= $account['balance'];
					$response .= "\n";
					$response .= $account['a_withdraw_fee'];
					$response .= "\n";
					$response .= $account['a_deposit_fee'];
					$response .= "\n";
					$response .= $account['a_delete_fee'];
					$response .= "\n";
					$response .= $this->ipsclass->my_nl2br($account['a_name']);
					$response .= "\n";
					$response .= $this->ipsclass->get_date($account['a_open'],'LONG');
					$response .= "\n";
					$response .= $this->ipsclass->numbers->format_currency($account['balance']);
					$response .= "\n";
					$response .= $this->_interest_time($account['a_interest_time']);
					$response .= "\n";
					$response .= $this->ipsclass->numbers->do_number_format($account['interest_rate'],10);
					$response .= "\n";
					$response .= $this->ipsclass->get_date($account['a_last_interest'],'LONG');
					$response .= "\n";
					$response .= $this->ipsclass->numbers->do_number_format($account['a_withdraw_fee'],10)."%";
					$response .= "\n";
					$response .= $this->ipsclass->numbers->do_number_format($account['a_deposit_fee'],10)."%";
					$response .= "\n";
					$response .= $this->ipsclass->numbers->format_currency($account['a_delete_fee']);
				}
				else
				{
					$response .= "error";
				}
			}
			elseif($this->ipsclass->input['info'] == 'transactions')
			{
				$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
				
				$info = $this->_revert_id($this->ipsclass->input['account']);
				
				if($info['member'] == $this->ipsclass->member['id'] && $account = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$info['account']." AND a_random=".$info['random']." AND a_mem_id=".$info['member'])))
				{
					$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_bank_transactions','where'=>"l_account_id='".$this->ipsclass->input['account']."'",'order'=>'l_timestamp DESC','limit'=>array($this->ipsclass->input['st'],5)));
					$this->ipsclass->DB->exec_query();
					
					$response .= $this->ipsclass->DB->get_num_rows();
					
					while($transaction = $this->ipsclass->DB->fetch_row())
					{
						$response .= "\n";
						$response .= $this->ipsclass->get_date($transaction['l_timestamp'],'LONG');
						$response .= "\n";
						$response .= $this->ipsclass->lang['bank_transaction_'.$transaction['l_action']];
						$response .= "\n";
						$response .= $transaction['l_action'] == 'create' ? "---" : $this->ipsclass->vars['market_prefix'].$this->ipsclass->do_number_format($transaction['l_amount']).$this->ipsclass->vars['market_suffix'];
						$response .= "\n";
						$response .= $this->ipsclass->numbers->format_currency($transaction['l_fee']);
						$response .= "\n";
						$response .= $this->ipsclass->numbers->format_currency($transaction['l_balance']);
					}
				}
			}
		}
		
		$ajax = $this->ipsclass->load_class(KERNEL_PATH.'class_ajax.php',"class_ajax");
		$ajax->class_init();
		$ajax->return_string($response);
	}
	
	function _build_bank_account_id($account_id,$random_number,$member_id)
	{
		return sprintf("%08d-%06d-%06d",$member_id,$random_number,$account_id);
	}
	
	function _revert_id($id)
	{
		list($member_id,$random,$account_id) = explode("-",$id);
		$ids = array();
		$ids['member'] = intval(ltrim($member_id,"0"));
		$ids['random'] = intval(ltrim($random,"0"));
		$ids['account'] = intval(ltrim($account_id,"0"));
		
		return $ids;
	}
	
	function _log_transaction($account_id, $action, $amount, $balance, $fee, $time = -1)
	{
		$this->ipsclass->DB->do_insert('market_bank_transactions',array(
																			'l_account_id' => $account_id,
																			'l_timestamp' => $time == -1 ? time() : $time,
																			'l_ip_address' => $this->ipsclass->ip_address,
																			'l_action' => $action,
																			'l_amount' => $amount,
																			'l_balance' => $balance,
																			'l_fee' => $fee,
										)								);
	}
	
	function _interest_time($seconds = 0)
	{
		$length = $seconds;
		$time = array();
		if($length >= 604800)
		{
			if($length >= 1209600)
			{
				$time[] = sprintf($this->ipsclass->lang['bank_weeks'],floor($length/604800));
			}
			else
			{
				$time[] = $this->ipsclass->lang['bank_week'];
			}
			$length %= 604800;
		}
		if($length >= 86400)
		{
			if($length >= 172800)
			{
				$time[] = sprintf($this->ipsclass->lang['bank_days'],floor($length/86400));
			}
			else
			{
				$time[] = $this->ipsclass->lang['bank_day'];
			}
			$length %= 86400;
		}
		if($length >= 3600)
		{
			if($length >= 7200)
			{
				$time[] = sprintf($this->ipsclass->lang['bank_hours'],floor($length/3600));
			}
			else
			{
				$time[] = $this->ipsclass->lang['bank_hour'];
			}
			$length %= 3600;
		}
		if($length >= 60)
		{
			if($length >= 120)
			{
				$time[] = sprintf($this->ipsclass->lang['bank_minutes'],floor($length/60));
			}
			else
			{
				$time[] = $this->ipsclass->lang['bank_minute'];
			}
			$length %= 60;
		}
		if($length >= 2)
		{
			$time[] = sprintf($this->ipsclass->lang['bank_seconds'],$length/60);
		}
		elseif($length == 1)
		{
			$time[] = $this->ipsclass->lang['bank_second'];
		}
		
		return implode(", ",$time);
	}
}
?>